#include<iostream>
#include<vector>
using namespace std;

//int smallestDifference(vector<int>& a, vector<int>& b) {
//    long long sub = (long long)a[0] - b[0];
//    long long ans = abs(sub);
//    for (int i = 0; i < a.size(); i++)
//    {
//        for (int j = 0; j < b.size(); j++)
//        {
//            if (abs((long long)a[i] - b[j]) < ans)
//                ans = abs((long long)a[i] - b[j]);
//        }
//    }
//    return ans;
//}
#include<algorithm>
//int smallestDifference(vector<int>& a, vector<int>& b) {
//    vector<int> v(a.begin(), a.end());
//    for (auto e : b)
//        v.push_back(e);
//    sort(v.begin(), v.end());
//    int min = v[0], max = v[1];
//    long long ans = abs((long long)max - min);
//    for (int i = 2; i < v.size(); i++)
//    {
//        max = v[i], min = v[i - 1];
//        if (abs((long long)max - min) < ans)
//            ans = abs((long long)max - min);
//    }
//    return ans;
//}

int smallestDifference(vector<int>& a, vector<int>& b) {
    for (int i = 0; i < a.size(); i++)
    {
        for (int j = 0; j < b.size(); j++)
        {
            if (abs((long long)a[i] - b[j]) == 16)
            {
                cout << endl;
            }
        }
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    vector<int> v(a.begin(), a.end());
    for (auto e : b)
        v.push_back(e);
    int cura = 0, curb = a.size();
    int flag = 1;
    long long ans = abs((long long)v[cura] - v[curb]);
    cura = 1;
    while (cura < a.size() && curb < v.size())
    {
        if (abs((long long)v[cura] - v[curb]) < ans)
        {
            ans = abs((long long)v[cura] - v[curb]);
            if (flag == 1)
                cura++;
            else
                curb++;
        }
        else
        {
            flag = -flag;
            if (flag == 1)
                cura++;
            else
                curb++;
        }
    }
    return ans;
}

int main()
{
    vector<int> a{ 1,2,3,4,5,6,7,8,9,6152,11,524,14,36366,16,24,26,21019,30,31,35,40,46121,42,44,63027,565,54,53,60981,56,59,61,63,67,23620,74,76,77,80,82,4180,87,90,91,608,61540,101,65124,118,39546,133,140,142,3217,29844 };
    vector<int> b{ 23702,2710,663,3742,9895,54444,172,6829,1712,17073,62650,9924,12484,714,715,2255,210,211,63701,37591,217,5850,3810,228,48874,8945,7410,49907,763,46333,42754,775,2824,30987,32532,790,32534,41752,287,25377,5411,291,24872,303,820,2870,828,55101,6975,320,843,847,45396,43349,21847,861,877,45935,61295,48496,1919,33152,4490,397,911,38288,402,23957,7070,930,54181,26538,23466,1963,1458,448,1484,35794,31701,45015,476,6625,46062,4080,508 };
    int ret = smallestDifference(a, b);

    //vector<int> c(a.begin(), a.end());
	return 0;
}